import {createWebHashHistory,createRouter} from "vue-router";

import index from '../view/index.vue'
import userlogin from '../view/userlogin.vue'
import adlogin from '../view/adlogin.vue'
import resident from '../view/resident.vue'
import house from '../view/house.vue'
import family from '../view/family.vue'
import login from '../view/login.vue'
import land from '../view/land.vue'
import subsidy from '../view/subsidy.vue'
import public1 from '../view/public1.vue'
import re from '../view/re.vue'
import ee from '../view/ee.vue'
import userindex from '../view/userindex.vue'
import user from '../view/user/userlogin.vue'
import useree from '../view/user/ee.vue'
import sd from '../view/statisticsdashboard.vue'



const routes = [
    // 添加根路径重定向
    {
        path: '/',
        redirect: '/login'
    },
    {
        path: '/login',
        component: login,
    },
    {
        path: '/index',
        name: 'index',
        component: index,
        children: [
            {
                path: '/index/userlogin',
                component: userlogin,
            },
            {
                path:'/index/resident',
                component: resident,
            },
            {
                path: '/index/adlogin',
                component: adlogin,
            },
            {
                path: '/index/house',
                component: house,
            },
            {
                path: '/index/family',
                component: family,
            },
            {
                path: '/index/land',
                component: land,
            },
            {
                path: '/index/subsidy',
                component: subsidy,
            },
            {
                path: '/index/public',
                component: public1,
            },
            {
                path: '/index/re',
                component: re,
            },
            {
                path: '/index/ee',
                component: ee,
            },
            {
                path: '/index/sd',
                component: sd,
                name: 'statistics'
            }

        ]
    },
    {
        path: '/userindex',
        name: 'userindex',
        component: userindex,
        children: [
            {
            path: '/userindex/userlogin',
            component: user,
            },
            {
                path: '/userindex/ee',
                component: useree
            }
            ]


    }
]

const router = createRouter({//创建路由实例
    history: createWebHashHistory(),//创建一个hash模式路由
    routes//路由表
})
// 添加路由守卫，在进入根路径时清除token
router.beforeEach((to, from, next) => {
    // 当访问根路径时清除token
    if (to.path === '/') {
        // 清除localStorage中的token
        localStorage.removeItem('token')
        // 清除sessionStorage中的token（如果使用了的话）
        sessionStorage.removeItem('token')
    }
    next()
})
// 修改导航守卫，处理index和userindex及其子页面之间的跳转
router.beforeEach((to, from, next) => {
    // 获取当前路由和来源路由的父级名称
    const getParentName = (route) => {
        // 如果是子路由，父级name为路由记录中的parent.name
        // 如果是顶级路由，直接返回自身name
        return route.matched.length > 1 ? route.matched[0].name : route.name//获取父级名称
    }

    const fromParent = getParentName(from)
    const toParent = getParentName(to)

    // 判断是否在index和userindex两个父级路由之间跳转（包括它们的子页面）
    const isBetweenIndexAndUserIndex =
        (fromParent === 'index' && toParent === 'userindex') ||
        (fromParent === 'userindex' && toParent === 'index')

    if (isBetweenIndexAndUserIndex) {
        // 清除token
        localStorage.removeItem('token')
        sessionStorage.removeItem('token')
    }

    next() // 继续路由跳转
})

export default router;